<?php
/**
 * Create by 11li
 * Date 2020/1/11
 * Time 20:43
 *
 */
namespace app\api\controller\v3;

use app\common\controller\Api;
use app\common\model\AgentShop;
use think\Cache;
use think\Db;
use app\admin\model\User as UserModel;

class Index extends Api
{
	
	protected $noNeedLogin = ['*'];
	
	/**
	 * 获取火爆店
	 */
	public function agent() {
		$postData = $this->request->post();
		$str =$this->request->path().json_encode($postData) ;
		$key = md5($this->request->path().json_encode($postData));
		
		if(!$data = Cache::get($key)) {
			$role = $this->request->post('level', UserModel::LEVEL_SHOP, 'intval');
			
			if(!in_array($role, [UserModel::LEVEL_WAREHOUSE, UserModel::LEVEL_SHOP])) {
				$role = UserModel::LEVEL_SHOP;
			}
			$page = $this->request->post('page', 1, 'intval');
			
			$lat = $this->request->post('lat');
			$lng = $this->request->post('lng');
			
			
			$where =[
				'role' => $role,
				'area' => $this->area,
				'status' => AgentShop::STATUS_Y
			];
			
			$obj = Db::name('agentShop');
			
			$total = $obj
				->where($where)
				->count();
			
			$field = 'id, name, image, content, mobile, lat, lng, address, role, update_time, ';
			if($lat && $lng) {
				$field .= ' ROUND(6378.138 * 2 * ASIN(SQRT( POW( SIN( ('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW( SIN(('.$lng.' * PI() / 180 - lng * PI() / 180 ) / 2 ),  2))) * 1000) AS distance';
			} else {
				$field .= '0 as distance';
			}
			
			$agents = $obj
				->where($where)
				->field($field)
				->order(['distance' => 'asc', 'update_time' => 'desc'])
				->page($page)
				->select();
			
			if($agents) {
				foreach ($agents as &$agent) {
					$agent['distance'] = get_distance($agent['distance']);
				}
			}
			
			$data = [
				'total' => $total,
				'list' => $agents,
				'key' => $key
			];
			
			Cache::set($key, $data, 600);
		}

		$this->success('查询成功', $data);
	}
	
	/**
	 * 地图的火爆点
	 */
	public function agentMap() {
		$postData = $this->request->post();
		$key = $this->request->path().md5(json_encode($postData));
		
		if(!$list = Cache::get($key)) {
			$lat = $this->request->post('lat');
			$lng = $this->request->post('lng');
			$where =[
				'role' => ['in', [UserModel::LEVEL_WAREHOUSE, UserModel::LEVEL_SHOP]],
				'area' => $this->area,
				'status' => AgentShop::STATUS_Y,
//				'distance' => ['gt', 10000]
			];
			
			$obj = Db::name('agentShop');
			$field = 'id, name, image, content, mobile, lat, lng, address, role, update_time, ';
			if($lat && $lng) {
				$field .= ' ROUND(6378.138 * 2 * ASIN(SQRT( POW( SIN( ('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW( SIN(('.$lng.' * PI() / 180 - lng * PI() / 180 ) / 2 ),  2))) * 1000) AS distance';
			} else {
				$field .= '0 as distance';
			}
			
			$agents = $obj
				->where($where)
				->field($field)
				->order(['distance' => 'asc', 'update_time' => 'desc'])
//				->having('distance < 10000')
//				->limit(50)
				->select();
			
			$list = [];
			if($agents) {
				$index = 0;
				foreach ($agents as $agent) {
					$list[] = [
						'id' => $index,
						'latitude' => $agent['lat'],
						'longitude' => $agent['lng'],
						'iconPath' => $agent['role'] == 3 ?'/static/location_shop.png' :'/static/location.png',
						'width' => 30,
						'height' => 30,
						'title' => $agent['name'].'('.$agent['mobile'].')',
					];
					$index ++;
				}
			}
			Cache::set($key, $list, 600);
		}
		
		$this->success('查询成功', $list);
	}
}